using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Repository.Conventions;
using NHibernate.Linq;

namespace Framework.Repository
{
    public sealed class AutoMappingAssemblyProvider : IAutoMappingTypeProvider
    {
        private readonly HashSet<Type> entityTypes = new HashSet<Type>();
        private readonly HashSet<Type> subClassTypes = new HashSet<Type>();
        private readonly HashSet<Type> overrideTypes = new HashSet<Type>();

        public AutoMappingAssemblyProvider AddEntityAssemblyOfType<T>()
        {
            var exportedTypes = typeof(T).Assembly.GetExportedTypes();
            exportedTypes.Where(AutoMappingHelper.IsEntity).ForEach(et => entityTypes.Add(et));
            exportedTypes.Where(AutoMappingHelper.IsSubClass).ForEach(et => subClassTypes.Add(et));
            exportedTypes.Where(AutoMappingHelper.IsAutoMappingOverrideType).ForEach(et => overrideTypes.Add(et));

            return this;
        }

        public IEnumerable<Type> GetEntityTypes()
        {
            return entityTypes;
        }

        public IEnumerable<Type> GetSubClassTypes()
        {
            return subClassTypes;
        }

        public IEnumerable<Type> GetOverrideTypes()
        {
            return overrideTypes;
        }
    }
}